<?php 
/*this script belongs to show  the scheme details */
 $path = drupal_get_path('module', 'scheme');
 drupal_add_css($path . '/css/scheme.css');
 drupal_add_js($path . '/js/scheme.js');

function scheme(){
 global $base_url;
 global $language;
	drupal_set_title(scheme);
 $output = '<div class="rsslogo">' . get_rss_feed("scheme_rssfeed") . '</div>	';
 $output .= '<div id="viewmod_schemes_tab_'.$language->language.'">
		<div class="menu_tab view-header">
			<div class="department_wise">
				<a class="view_head_sel" href="'.$base_url.'/scheme/department_wise/">'.t('Department wise').'</a>		
			</div>
		    <div class="beneficiary_wise">				
				<a class="inactive"  href="'.$base_url.'/scheme/beneficiary_wise">'.t('Beneficiary Wise').'</a>
			</div>		
			<div class="atoz_view">				
				<a class="inactive" href="'.$base_url.'/scheme/alpha_view">'.t('A to Z').'</a>
			</div>		
		    <div class="search">				
				<a class="inactive"  href="'.$base_url.'/scheme/search">'.t('Search').'</a>
			</div>
		</div>		
		<div class="clear"></div>		
	</div>';
	
			
	my_db_change(1);
	
	$result = db_query("SELECT distinct dep_id,department_name FROM scheme_dept WHERE language = '$language->language' ORDER BY department_name ASC"); 
	my_db_change(0);
		  
	  $output .= '<div class="result_inner">';	  
	  foreach($result as $item) {	  
	  	$output .= '<div class="scheme_list"><p><a  href="'.$base_url.'/scheme/department_wise/'.$item->dep_id.'">'.t(substr($item->department_name,0,255)).'</a></p></div>'; 
	  }
	  $output .= '</div>';	
	  	  
	$form['field'] = array(
        '#type' => 'fieldset',
        '#title' => '',
        '#description' => $output,
        '#tree' => TRUE,
    );
	return $form;
}
function dept_wise($deptId='') {
 global $base_url,$language;
  $output = '<div class="rsslogo">' . get_rss_feed("scheme_rssfeed") . '</div>	';
 $output .= '<div id="viewmod_schemes_tab_'.$language->language.'">
		<div class="menu_tab view-header">
			<div class="department_wise">
				<a class="view_head_sel" href="'.$base_url.'/scheme/department_wise/">'.t('Department wise').'</a>		
			</div>
		    <div class="beneficiary_wise">				
				<a class="inactive"  href="'.$base_url.'/scheme/beneficiary_wise">'.t('Beneficiary Wise').'</a>
			</div>		
			<div class="atoz_view">				
				<a class="inactive"  href="'.$base_url.'/scheme/alpha_view">'.t('A to Z').'</a>
			</div>		
		    <div class="search">				
				<a class="inactive"  href="'.$base_url.'/scheme/search">'.t('Search').'</a>
			</div>
		</div>		
		<div class="clear"></div>		
	</div>';
	if(!empty($deptId)){
		$cond = " AND b.field_scheme_concern_department_value = '" . (string)$deptId . "'";
	}
	global $language;	
	$empty ='';
//	print_r($dep);
	my_db_change(1);
	$lan_qry=db_query("select language from node a INNER JOIN content_type_scheme b ON a.nid =b.nid JOIN depart_mst AS c ON c.dep_id = CAST(b.field_scheme_concern_department_value AS INTEGER)")->fetchField();
	
	$result = db_query("SELECT c.dept_name AS heading FROM
			node AS a, content_type_scheme AS b, depart_mst AS c
			WHERE a.nid = b.nid AND a.vid = b.vid and (a.language='".$language->language."' or a.language = '') AND c.dep_id = 											            CAST(b.field_scheme_concern_department_value AS INTEGER) AND a.status = 1" . $cond .
			" ORDER BY trim(a.title) ASC")->fetchField();	
	
	/*$count_qry = "SELECT count(a.title) FROM
				node AS a, content_type_scheme AS b, depart_mst AS c
				WHERE a.nid = b.nid AND a.vid = b.vid AND c.dep_id = CAST(b.field_scheme_concern_department_value AS INTEGER) AND a.status = 1" . $cond;	
	$count=db_query($count_qry);
	foreach($count as $item) {	  
	//print_r($item->count);
	}
					
	my_db_change(0);		  
	$output .= '<div class="result_inner">';	  
	foreach($result as $item) {	  
	  	$output .= '<div class="scheme_list"><p><a  href="'.$base_url.'/scheme/data_view/'.$item->nid.'">'.t(substr($item->title,0,255)).'</a></p></div>'; 
	  }
	$output .= '</div>';
	*/
	
	$query = db_select('node', 'a');
		$query->join('content_type_scheme', 'b', 'a.nid =b.nid AND a.vid =b.vid AND b.field_scheme_concern_department_value = \'' .(string)$deptId .'\'');
		$query->join('depart_mst', 'c','CAST(b.field_scheme_concern_department_value as int)=  c.dep_id');
		$query->condition('a.language',$language->language);
		$query->condition('a.status','1');
		$query->fields('a',array('title','nid','vid'))->fields('b',array('field_scheme_concern_department_value','nid','vid'))
		->fields('c',array('dep_id','dept_name'))->orderBy('a.title', 'ASC')->distinct('a.title','c.dep_id','a.nid','b.nid','b.field_scheme_description_value');
		$query_result=$query->extend('PagerDefault')->limit(15)->execute();
	//print_r( $query_result);
	my_db_change(0);
	
	$output .= '<div class="result_inner">';	
	$output .= '<div class="heading_name"><h3>'.t(substr($result,0,255)).'</h3></div>'; 		  
	foreach($query_result as $item) {
		//print_r($item);
		$deptname= $item->dept_name;
		$title=$item->title;
		$output .= '<div class="scheme_list"><p><a  href="'.$base_url.'/scheme/data_view/'.$item->nid.'">'.substr($title,0,255).'</a></p></div>'; 
	}	
	//$output .= '<div class="heading_name"><h3>'.substr($deptname,0,255).'</h3></div>';
	$output .= theme('pager');	  
	$output .= '</div>';	
		
	$form['field'] = array(
        '#type' => 'fieldset',
        '#title' => '',
        '#description' => $output,
        '#tree' => TRUE,
    );
	return $form;
}
 
function data_view($nid='') {
   global $base_url;
   global $language;
   $url = $base_url . '/' . drupal_get_path('module', 'scheme');
     if(!empty($department_id)){
		$dept_cond = "AND c.field_scheme_concern_department_value = '" . (string)$department_id . "' ";
	}
   $output = '<div class="clear-block"><div style="float:right;clear:both;margin-right:20px;"><a href="javascript:history.go(-1)"><strong>Back</strong></a></div>';
   $output .= '<div class ="node_view_heading" style="clear:both;">';
   $output .= '<div class ="node_view_head_content">';
   $output .= '<img height="15" width="15" style="display: none;" class="node_view_top_corner" alt="" src= "'.$url.'/images/leftcurve.jpg" ?> ';		  
   $output .= '<span class = "node_view_head_title"><br/>Scheme</span>';
   $output .= '</div></div><div class="clear"></div></div></div>';
   
	my_db_change(1);
	$qery = db_query("SELECT c.field_scheme_concern_department_value FROM content_type_scheme AS c where c.nid = $nid");
	foreach($qery as $row){
	$depid=$row->field_scheme_concern_department_value;
	}
	
	$qry = db_query("SELECT a.title,b.dept_name,c.nid FROM node AS a, depart_mst as b,content_type_scheme AS c WHERE a.nid = c.nid AND a.vid = c.vid AND c.nid = $nid AND b.dep_id = cast(c.field_scheme_concern_department_value as int) AND a.status = 1 ORDER BY trim(a.title)");
	foreach($qry as $row){
		$title=$row->title;
		$nid=$row->nid;
		$dept_name=$row->dept_name;	
		$node = node_load($row->nid);
	}
	//print "<pre>";		print_r($node);		print "</pre>";
	$lan_qry=db_query("select language,tnid from node where nid=$nid");
	foreach($lan_qry as $row){
	//print_r($row);
		$content_language=$row->language;
		$tnid=$row->tnid;
	}
	
	if($content_language=='en' || $content_language=='ta') {
	$qury=db_query("select n.title,d.dept_name,s.sponsor_name,bv.beneficiaries_name,sb.benefits_name,c.field_scheme_funding_pattern_value,c.field_scheme_eligible_community_value,c.field_scheme_introduced_on_value,c.field_scheme_associate_value,c.field_scheme_eligibility_value,c.field_scheme_description_value,	c.field_scheme_eligible_income_value,c.field_scheme_eligible_age_from_value,c.field_scheme_eligible_age_to_value,c.field_scheme_how_to_avail_value,c.field_scheme_org_value,c.field_scheme_type_value,dist.field_scheme_concern_district_value,f.filename,f.filepath,so.unpublish_on from node AS n join  content_type_scheme AS c on n.nid = c.nid and n.vid = c.vid and  n.status = 1 and n.language='$content_language' and n.tnid = $tnid and  n.nid =$nid join depart_mst AS d on d.dep_id = CAST(c.field_scheme_concern_department_value AS INTEGER) join scheme_sponsors AS s on s.id = CAST(c.field_scheme_sponsored_by_value AS INTEGER) join scheme_beneficiaries AS bv on bv.id = CAST(c.field_scheme_beneficiaries_value AS INT) join scheme_benefits AS sb on sb.id = CAST(c.field_scheme_benfits_type_value AS INT) join content_field_scheme_concern_district AS dist on dist.nid = c.nid and dist.vid = c.vid join scheduler AS so on  n.nid = so.nid left join files as f on c.field_scheme_upload_fid=f.fid ");
	}
	else {
	 $qury=db_query("select n.title,d.dept_name,s.sponsor_name,bv.beneficiaries_name,sb.benefits_name,c.field_scheme_funding_pattern_value,c.field_scheme_eligibility_value,c.field_scheme_introduced_on_value,c.field_scheme_associate_value,c.field_scheme_eligible_community_value,c.field_scheme_description_value,	c.field_scheme_eligible_income_value,c.field_scheme_eligible_age_from_value,c.field_scheme_eligible_age_to_value,c.field_scheme_how_to_avail_value,c.field_scheme_org_value,c.field_scheme_type_value,dist.field_scheme_concern_district_value,f.filename,f.filepath,so.unpublish_on from node AS n join  content_type_scheme AS c on n.nid = c.nid and n.vid = c.vid and  n.status = 1 and  n.nid =$nid join depart_mst AS d on d.dep_id = CAST(c.field_scheme_concern_department_value AS INTEGER) join scheme_sponsors AS s on s.id = CAST(c.field_scheme_sponsored_by_value AS INTEGER) join scheme_beneficiaries AS bv on bv.id = CAST(c.field_scheme_beneficiaries_value AS INT) join scheme_benefits AS sb on sb.id = CAST(c.field_scheme_benfits_type_value AS INT) join content_field_scheme_concern_district AS dist on dist.nid = c.nid and dist.vid = c.vid join scheduler AS so on  n.nid = so.nid left join files as f on c.field_scheme_upload_fid=f.fid ");
	}
	foreach($qury as $row){	
		$btitle=$row->title;
		$dept_namee=$row->dept_name;
		$sponsor_name=$row->sponsor_name;
		$beneficiaries_name=$row->beneficiaries_name;
		$benefits_name=$row->benefits_name;
		$paternval=$row->field_scheme_funding_pattern_value;
		$dval=$row->field_scheme_description_value;
		$incomeval=$row->field_scheme_eligible_income_value;
		$ageval=$row->field_scheme_eligible_age_from_value;
		$agetoval=$row->field_scheme_eligible_age_to_value;
		$paternval=$row->field_scheme_funding_pattern_value;
		$dval=$row->field_scheme_description_value;
		$incomeval=$row->field_scheme_eligible_income_value;
		$agefromval=$row->field_scheme_eligible_age_from_value;
		$agetoval=$row->field_scheme_eligible_age_to_value;
		$community_value=$row->field_scheme_eligible_community_value;
		$availval=$row->field_scheme_how_to_avail_value;
		$orgval=$row->field_scheme_org_value;		
		$schemtypeval=$row->field_scheme_type_value;
		$distt= $row->field_scheme_concern_district_value; 		
		$filename=$row->filename;
		$filepath=$row->filepath;
		$unpublish_on=$row->unpublish_on;
		$eligible=$row->field_scheme_eligibility_value;	
		$associateval=$row->field_scheme_associate_value;
		$intro=$row->field_scheme_introduced_on_value;
	}
	//print "<pre>";	
	//print $btitle;
	 if($distt!=0) { 
		  $sqry = db_query("select n.title,d.dt_name from node AS n JOIN district_mst AS d ON nid = $orgval and d.dt_cd in ($distt)"); 
	 } 
	 else {  
	    $sqry = db_query("select n.title from node AS n where n.nid = $orgval ");
		$dist_name = "All Districts";
	 }
	
	foreach($sqry as $row){  //print "<pre>"; print_r($row); print "</pre>"; 
     $orgname = $row->title;
    if($distt!=0){
	  $dist_name .= $row->dt_name.",";
	  }
	 } 
   if($distt!=0){
   $dist_name = substr($dist_name,0,-1);
   }
   if($community_value != ''){
     $com = db_query("select community_name from community_mst where com_id=$community_value");
	}
	foreach($com as $row){ 
		 $comm = $row->community_name;
	}
	my_db_change(0);
		 
	
	$output .= '<div class="node_viewlist_even">';	 
	$output .= '<span class = "left_column">'.t('Concerned Department').'</span>';
	$output .= '<span class = "middle_column"> : </span>';
	$output .= '<span class = "right_column">'.t($dept_name).'</span>';
	$output .= '<div class="clr"></div></div>';
	
	$output .= '<div class = "node_viewlist_odd">';
	$output .= '<span class = "left_column">'.t('Concerned District').'</span>';
	$output .= '<span class = "middle_column"> : </span>';
	$output .= '<span class = "right_column">'.t($dist_name).'</span>';
	$output .= '<div class="clr"></div>';
	$output .= '</div>';
	
	$output .= '<div class="node_viewlist_even">';	 
	$output .= '<span class = "left_column">'.t('Organisation Name').'</span>';
	$output .= '<span class = "middle_column"> : </span>';
	$output .= '<span class = "right_column">'.t($orgname).'</span>';
	$output .= '<div class="clr"></div></div>';
	
	$output .= '<div class = "node_viewlist_odd">';
	$output .= '<span class = "left_column">'.t('Scheme Details').'</span>';
	$output .= '<div class="clr"></div>';
	$output .= '</div>';
	
	$output .= '<div class = "node_viewlist_odd">';
	$output .= '<span class = "left_column"><span class="whitespace"></span>'.t('Title / Name').'</span>';
	$output .= '<span class = "middle_column"> : </span>';
	$output .= '<span class = "right_column">'.t($title).'</span>';
	$output .= '<div class="clr"></div>';
	$output .= '</div>';
	
	$output .= '<div class = "node_viewlist_odd">';
	$output .= '<span class = "left_column"><span class="whitespace"></span>'.t('Associated Scheme').'</span>';
	$output .= '<span class = "middle_column"> : </span>';
	$output .= '<span class = "right_column">'.$associateval.'</span>';
	$output .= '<div class="clr"></div>';
	$output .= '</div>';
	
	$output .= '<div class = "node_viewlist_odd">';
	$output .= '<span class = "left_column"><span class="whitespace"></span>'.t('Sponsored By').'</span>';
	$output .= '<span class = "middle_column"> : </span>';
	$output .= '<span class = "right_column">'.t($sponsor_name).'</span>';
	$output .= '<div class="clr"></div>';
	$output .= '</div>';
	
	$output .= '<div class = "node_viewlist_odd">';
	$output .= '<span class = "left_column"><span class="whitespace"></span>'.t('Funding Pattern').'</span>';
	$output .= '<span class = "middle_column"> : </span>';
	$output .= '<span class = "right_column">'.$paternval.'</span>';
	$output .= '<div class="clr"></div>';
	$output .= '</div>';
	
	$output .= '<div class="node_viewlist_even">';	 
	$output .= '<span class = "left_column">'.t('Beneficiaries').'</span>';
	$output .= '<span class = "middle_column"> : </span>';
	$output .= '<span class = "right_column">'.t($beneficiaries_name).'</span>';
	$output .= '<div class="clr"></div></div>';
	
	$output .= '<div class = "node_viewlist_odd">';
	$output .= '<span class = "left_column">'.t('Benefits Types').'</span>';
	$output .= '<span class = "middle_column"> : </span>';
	$output .= '<span class = "right_column">'.t($benefits_name).'</span>';
	$output .= '<div class="clr"></div>';
	$output .= '</div>';
	
	$output .= '<div class = "node_viewlist_even">';
	$output .= '<span class = "left_column">'.t('Eligibility criteria').'</span>';
	$output .= '<div class="clr"></div>';
	$output .= '</div>';
	
	$output .= '<div class = "node_viewlist_even">';
	$output .= '<span class = "left_column"><span class="whitespace"></span>'.t('Income').'</span>';
	$output .= '<span class = "middle_column"> : </span>';
	if ($income!=''){
		$output .='<span class = "right_column">'.$income.'</span>';
	}else{
		$output .='<span class = "right_column">--</span>';
	}
	$output .= '<div class="clr"></div>';
	$output .= '</div>';
	
	$output .= '<div class = "node_viewlist_even">';
	$output .= '<span class = "left_column"><span class="whitespace"></span>'.t('Age').'</span>';
	$output .= '<span class = "middle_column"> : </span>';
	$output .='<span class = "right_column">'.$agefromval.'--'.$agetoval.'</span>';
	$output .= '<div class="clr"></div>';
	$output .= '</div>';
	
	$output .= '<div class = "node_viewlist_even">';
	$output .= '<span class = "left_column"><span class="whitespace"></span>'.t('Community').'</span>';
	$output .= '<span class = "middle_column"> : </span>';
	if ($community_value!=''){
	$output .='<span class = "right_column">'.t($comm).'</span>';
	}
	else{
		$output .='<span class = "right_column">--</span>';
	}
	$output .= '<div class="clr"></div>';
	$output .= '</div>';
	
	$output .= '<div class = "node_viewlist_even">';
	$output .= '<span class = "left_column"><span class="whitespace"></span>'.t('Other Details').'</span>';
	$output .= '<span class = "middle_column"> : </span>';
	if ($eligible!=''){
	$output .='<span class = "right_column">'.t($eligible).'</span>';
	}
	else{
		$output .='<span class = "right_column">--</span>';
	}
	$output .= '<div class="clr"></div>';
	$output .= '</div>';
	
	$output .= '<div class = "node_viewlist_odd">';
	$output .= '<span class = "left_column">'.t('How To Avail').'</span>';
	$output .= '<span class = "middle_column"> : </span>';
	$output .= '<span class = "right_column">'.t($availval).'</span>';
	$output .= '<div class="clr"></div>';
	$output .= '</div>';
	
	$output .= '<div class = "node_viewlist_even">';
	$output .= '<span class = "left_column">'.t('Validity of the Scheme').'</span>';
	$output .= '<div class="clr"></div>';
	$output .= '</div>';
	
	$output .= '<div class = "node_viewlist_even">';
	$output .= '<span class = "left_column"><span class="whitespace"></span>'.t('Introduced On').'</span>';
	$output .= '<span class = "middle_column"> : </span>';
	$output .='<span class = "right_column">';
	$output .= format_date($intro, 'custom', 'M d, Y');
	$output .= '</span>';
	$output .= '<div class="clr"></div>';
	$output .= '</div>';
	
	$output .= '<div class = "node_viewlist_even">';
	$output .= '<span class = "left_column"><span class="whitespace"></span>'.t('Valid Upto').'</span>';
	$output .= '<span class = "middle_column"> : </span>';
	$output .='<span class = "right_column">';
	if($unpublish_on != ''){
	$output .= format_date($unpublish_on, 'custom', 'M d, Y');
	}
	else{
		$output .='<span class = "right_column">--</span>';
	}
	$output .= '</span>';
	$output .= '<div class="clr"></div>';
	$output .= '</div>';
	
	$output .= '<div class = "node_viewlist_even">';
	$output .= '<span class = "left_column"><span class="whitespace"></span>'.t('Description').'</span>';
	$output .= '<span class = "middle_column"> : </span>';
	$output .='<span class = "right_column">'.t($dval).'</span>';
	$output .= '<div class="clr"></div>';
	$output .= '</div>';
	
	if($schemtypeval == 'download' || $schemtypeval == 'online')
	{	
		$output .= '<div class = "node_viewlist_odd">';
		$output .= '<span class = "left_column">'.t('Scheme Type').'</span>';
		$output .= '<span class = "middle_column"> : </span>';
		$output .= '<span class = "right_column">'.t($schemtypeval).'</span>';
		$output .= '<div class="clr"></div>';
		$output .= '</div>';
	}
	
	$filepat="'$filepath'";
	
	if($schemtypeval == 'download')
	{
		$output .= '<div class = "node_viewlist_odd">';
		$output .= '<span class = "left_column"><span class="whitespace"></span>'.t('Uploaded File').'</span></span>';
		$output .= '<span class = "middle_column"> : </span>';
		$output .= '<span class = "right_column">';
		$output .= '<a href="javascript:void(0);" onClick="return tnportal_window(' . $filepat . ');">'.t($filename).'</a>';
		$output .= '</span>';
		$output .= '<div class="clr"></div>';
		$output .= '</div>';
	}
	if($schemtypeval == 'online')
	{
		$output .= '<div class = "node_viewlist_odd">';
		$output .= '<span class = "left_column"><span class="whitespace"></span>'.t('Reference URL').'</span></span>';
		$output .= '<span class = "middle_column"> : </span>';
		$output .= '<span class = "right_column"><a href="javascript:void(0);" onClick="return tnportal_ext_window('.$filepat.');">'.t($filepath).'</a></span>';
		$output .= '<div class="clr"></div>';
		$output .= '</div>';
	}
	$output .= '<div class ="node_view_bottom">';
	$output .= '<div class ="node_view_bottom_content">';
	$output .= '<img height="15" width="15" style="display: none;" class="node_view_bottom_corner" alt="" src= "'.$url.'/images/bottomleft.jpg" ?>';
	$output .= '</div></div>';
	
	$form['field'] = array(
			'#type' => 'fieldset',
			'#title' => '',
			'#description' => $output,
			'#tree' => TRUE,
		);
	return $form;
 }
function get_rss_feed($links) {
	global $base_url;
	
	$http = 'http://';
	if(isset($_SERVER['HTTPS']))
	$http = 'https://';
	
	
	$cmsbaseurl = $http.$_SERVER['HTTP_HOST']."/tnportal_latest/";
	
	$rssicon = '<a href="'.$cmsbaseurl.$links.'" target="_blank"><img src="'.$base_url.'/sites/all/modules/scheme/images/rss_feed_icon.gif" alt="RSS Icon" /></a>';
	return $rssicon;
}

?>